<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="告警级别" prop="alarmLevel">
        <el-select v-model="queryParams.alarmLevel" placeholder="请选择告警级别" clearable>
          <el-option label="紧急" value="1" />
          <el-option label="重要" value="2" />
          <el-option label="一般" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="处理状态" prop="handleStatus">
        <el-select v-model="queryParams.handleStatus" placeholder="请选择处理状态" clearable>
          <el-option label="未处理" value="0" />
          <el-option label="已处理" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="告警时间">
        <el-date-picker
          v-model="daterangeAlarmTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleBatchProcess"
          v-hasPermi="['energy:alarm:edit']"
        >批量处理</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="alarmRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备名称" align="center" prop="deviceName" :show-overflow-tooltip="true" />
      <el-table-column label="告警内容" align="center" prop="alarmContent" :show-overflow-tooltip="true" />
      <el-table-column label="告警级别" align="center" prop="alarmLevel">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.alarmLevel === '1'" type="danger">紧急</el-tag>
          <el-tag v-if="scope.row.alarmLevel === '2'" type="warning">重要</el-tag>
          <el-tag v-if="scope.row.alarmLevel === '3'" type="info">一般</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="告警值" align="center" prop="alarmValue" />
      <el-table-column label="阈值" align="center" prop="thresholdValue" />
      <el-table-column label="告警时间" align="center" prop="alarmTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.alarmTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理状态" align="center" prop="handleStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.handleStatus === '0'" type="danger">未处理</el-tag>
          <el-tag v-if="scope.row.handleStatus === '1'" type="success">已处理</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="处理人" align="center" prop="handleBy" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.handleStatus === '0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleProcess(scope.row)"
            v-hasPermi="['energy:alarm:edit']"
          >处理</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 处理告警对话框 -->
    <el-dialog :title="processTitle" :visible.sync="processOpen" width="500px" append-to-body>
      <el-form ref="processForm" :model="processForm" label-width="80px">
        <el-form-item label="设备名称">
          <el-input v-model="processForm.deviceName" disabled />
        </el-form-item>
        <el-form-item label="告警内容">
          <el-input v-model="processForm.alarmContent" type="textarea" disabled />
        </el-form-item>
        <el-form-item label="处理结果" prop="handleResult">
          <el-input v-model="processForm.handleResult" type="textarea" placeholder="请输入处理结果" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitProcess">确 定</el-button>
        <el-button @click="processOpen = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看告警详情对话框 -->
    <el-dialog title="告警详情" :visible.sync="viewOpen" width="600px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="设备名称">{{ viewForm.deviceName }}</el-descriptions-item>
        <el-descriptions-item label="设备编码">{{ viewForm.deviceCode }}</el-descriptions-item>
        <el-descriptions-item label="告警内容">{{ viewForm.alarmContent }}</el-descriptions-item>
        <el-descriptions-item label="告警级别">
          <el-tag v-if="viewForm.alarmLevel === '1'" type="danger">紧急</el-tag>
          <el-tag v-if="viewForm.alarmLevel === '2'" type="warning">重要</el-tag>
          <el-tag v-if="viewForm.alarmLevel === '3'" type="info">一般</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="告警值">{{ viewForm.alarmValue }}</el-descriptions-item>
        <el-descriptions-item label="阈值">{{ viewForm.thresholdValue }}</el-descriptions-item>
        <el-descriptions-item label="告警时间">{{ parseTime(viewForm.alarmTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</el-descriptions-item>
        <el-descriptions-item label="处理状态">
          <el-tag v-if="viewForm.handleStatus === '0'" type="danger">未处理</el-tag>
          <el-tag v-if="viewForm.handleStatus === '1'" type="success">已处理</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="处理人">{{ viewForm.handleBy || '暂无' }}</el-descriptions-item>
        <el-descriptions-item label="处理时间">{{ viewForm.handleTime ? parseTime(viewForm.handleTime, '{y}-{m}-{d} {h}:{i}:{s}') : '暂无' }}</el-descriptions-item>
        <el-descriptions-item label="处理结果" :span="2">{{ viewForm.handleResult || '暂无' }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
// 这里暂时使用模拟数据，实际项目中需要创建对应的API接口
export default {
  name: "AlarmRecord",
  data() {
    return {
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      alarmRecordList: [],
      processTitle: "",
      processOpen: false,
      viewOpen: false,
      daterangeAlarmTime: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceName: null,
        alarmLevel: null,
        handleStatus: null,
        beginTime: null,
        endTime: null
      },
      processForm: {},
      viewForm: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 模拟数据 - 实际项目中应该调用后端API
    getList() {
      this.loading = true;
      // 模拟API响应
      setTimeout(() => {
        this.alarmRecordList = [
          {
            recordId: 1,
            deviceName: '变压器#1',
            deviceCode: 'DEV001',
            alarmContent: '设备温度超过80度，当前温度: 85.5度',
            alarmLevel: '1',
            alarmValue: 85.5,
            thresholdValue: 80.0,
            alarmTime: new Date('2024-01-20 14:30:00'),
            handleStatus: '0',
            handleBy: null,
            handleTime: null,
            handleResult: null
          },
          {
            recordId: 2,
            deviceName: '配电柜#1',
            deviceCode: 'DEV002',
            alarmContent: '设备电流异常，当前电流: 55.2A',
            alarmLevel: '2',
            alarmValue: 55.2,
            thresholdValue: 50.0,
            alarmTime: new Date('2024-01-20 13:15:00'),
            handleStatus: '1',
            handleBy: 'admin',
            handleTime: new Date('2024-01-20 13:45:00'),
            handleResult: '已检查设备，负载正常，临时性超标'
          }
        ];
        this.total = this.alarmRecordList.length;
        this.loading = false;
      }, 500);
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.daterangeAlarmTime) {
        this.queryParams.beginTime = this.daterangeAlarmTime[0];
        this.queryParams.endTime = this.daterangeAlarmTime[1];
      } else {
        this.queryParams.beginTime = null;
        this.queryParams.endTime = null;
      }
      this.getList();
    },
    resetQuery() {
      this.daterangeAlarmTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.recordId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleProcess(row) {
      this.processForm = Object.assign({}, row);
      this.processForm.handleResult = '';
      this.processOpen = true;
      this.processTitle = "处理告警";
    },
    handleBatchProcess() {
      this.$modal.msgInfo("批量处理功能开发中...");
    },
    submitProcess() {
      if (!this.processForm.handleResult) {
        this.$modal.msgError("请输入处理结果");
        return;
      }
      
      // 这里应该调用后端API处理告警
      this.$modal.msgSuccess("告警处理成功");
      this.processOpen = false;
      this.getList();
    },
    handleView(row) {
      this.viewForm = Object.assign({}, row);
      this.viewOpen = true;
    }
  }
};
</script> 